<?php
/**
 * @author Artur Bier <izatop@gmail.com>
 * @package Files.Types
 *
 * @copyright TinyWEB CMS
 * @link http://tinywebcms.ru
 * @link http://cms.vingrad.ru
 * @version SVN: $Id$ 
 */
 
/**
 * FilesTypeFilter
 */
abstract class FilesTypeFilter extends Files
{
    protected $types;
    protected $lastType;
    
    public function __construct($types = array())
    {
        $this->defineTypes($types);
    }
    
    final public function defineTypes($types = array()) 
    {
        $this->types = array_flip($types);
    }
    
    final public function getLastType()
    {
        return $this->lastType;
    }
    
    final public function check($file)
    {
        exec('/usr/bin/file -ib ' . escapeshellarg($file), $output, $code);

        if($code < 0) {
            throw new Exception('Files: can\'t check type for ' . $file . "\n" . implode("\n", $output));
        }
        
        $this->lastType = reset($output);
        if(!array_key_exists($this->lastType, $this->types)) {
            return false;
        }
        
        return true;
    }
}

/**
 * Tab: 4 spaces
 * Encoding: utf-8
 */
?>